<?php

declare(strict_types=1);

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;

$middleware = function (ServerRequestInterface $request, RequestHandlerInterface $handler) use ($app) {
    $response = $handler->handle($request);
    // $response = $app->getResponseFactory()->createResponse();
    $data = $response->getBody();
    error_log('-----data: ' . $data->__tostring()); // ---data: Hello
    $response->getBody()->write('World');
    return $response;
};

$app->get('/', function (ServerRequestInterface $request, ResponseInterface $response) {
    $response->getBody()->write('Hello ');
    return $response;
})->add($middleware);